
package com.dstz.form.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.core.util.BeanUtils;
import com.dstz.base.core.util.StringUtil;

/**
 * @author xianggang
 * @date 2016年4月18日
 * 
 */
public class JSONObjectUtil {
	
	public static JSONObject getJSONObjectTrue() {
		JSONObject jo = new JSONObject();
		jo.put("success", "true");
		return jo;
	}
	
	public static JSONObject getJSONObjectTrue(String key, Object value) {
		JSONObject jo = new JSONObject();
		jo.put("success", "true");
		jo.put(key, value);
		return jo;
	}
	
	public static JSONObject getJSONObjectFalse() {
		JSONObject jo = new JSONObject();
		jo.put("success", "false");
		return jo;
	}
	
	public static JSONObject getJSONObjectFalse(String msg) {
		JSONObject jo = new JSONObject();
		jo.put("success", "false");
		jo.put("msg", msg);
		return jo;
	}
	
	public static JSONObject parseExpand(String expand) {
		JSONObject parseObject = null;
		try {
			parseObject = JSONObject.parseObject(expand);
		} catch (Exception e) {
			try {
				parseObject = JSONObject
				        .parseObject(SqlUtil.resetQuoteStr(StringUtil.resetJsonStr(StringUtil.quotToStr(expand))));
			} catch (Exception e1) {
				try {
					parseObject = JSONObject.parseObject(SqlUtil.resetQuoteStr(StringUtil.resetJsonStr(expand)));
				} catch (Exception e2) {
					parseObject = JSONObject.parseObject(StringUtil.resetJsonStr(expand));
				}
			}
			
		}
		
		return parseObject;
	}
	
	public static String getValByKey(String key, String jsonStr, JSONObject jsonObj) {
		
		if (StringUtil.isEmpty(key) || StringUtil.isEmpty(jsonStr)) {
			return "";
		}
		if (jsonObj == null) {
			return "";
		}
		
		String[] keys = key.split("\\.");
		
		int len = keys.length;
		
		String curKey = keys[0];
		
		String curData = "";
		
		int arrIdx = 0;
		boolean isArray = false;
		if (curKey.indexOf("[") > 0) {
			String[] split = curKey.split("\\[");
			curKey = split[0];
			arrIdx = Integer.parseInt(split[1].replace("]", ""));
			isArray = true;
		}
		
		if (jsonObj.containsKey(curKey)) {
			
			curData = jsonObj.getString(curKey);
		}
		
		if (StringUtil.isEmpty(curData)) {
			return "";
		}
		
		if (len == 1) {
			
			return curData;
			
		} else {
			
			StringBuffer sb = new StringBuffer();
			
			for (int i = 1; i < keys.length; i++) {
				
				sb.append(keys[i]);
				
				if (i < keys.length) {
					
					sb.append(".");
					
				}
			}
			if (isArray) {
				try {
					curData = JSONArray.parseObject(curData).getString(arrIdx + "");
				} catch (Exception e) {
				}
			}
			
			return getValByKey(sb.toString(), curData);
			
		}
		
	}
	
	public static String getValByKey(String key, String jsonStr) {
		
		if (StringUtil.isEmpty(key) || StringUtil.isEmpty(jsonStr)) {
			return "";
		}
		JSONObject jsonObj = null;
		try {
			jsonObj = JSONObject.parseObject(StringUtil.resetJsonStr(jsonStr));
		} catch (Exception e) {
			jsonObj = JSONObject.parseObject(jsonStr);
		}
		
		if (BeanUtils.isEmpty(jsonObj)) {
			return "";
		}
		
		String[] keys = key.split("\\.");
		
		int len = keys.length;
		
		String curKey = keys[0];
		
		String curData = "";
		
		int arrIdx = 0;
		boolean isArray = false;
		if (curKey.indexOf("[") > 0) {
			String[] split = curKey.split("\\[");
			curKey = split[0];
			arrIdx = Integer.parseInt(split[1].replace("]", ""));
			isArray = true;
		}
		
		if (jsonObj.containsKey(curKey)) {
			
			curData = jsonObj.getString(curKey);
		}
		
		if (StringUtil.isEmpty(curData)) {
			return "";
		}
		
		if (len == 1) {
			
			return curData;
			
		} else {
			
			StringBuffer sb = new StringBuffer();
			
			for (int i = 1; i < keys.length; i++) {
				
				sb.append(keys[i]);
				
				if (i < keys.length) {
					
					sb.append(".");
					
				}
			}
			if (isArray) {
				try {
					curData = JSONArray.parseObject(curData).getString(arrIdx + "");
				} catch (Exception e) {
				}
			}
			
			return getValByKey(sb.toString(), curData);
			
		}
		
	}
	
	/**
	 * 将对象转成JSON字符串
	 * 
	 * @param obj
	 * @return
	 * @return String
	 * @author xianggang
	 * @date: 2016年11月25日 下午12:05:28
	 */
	public static String toJsonStr(Object obj) {
		return JSON.toJSONString(obj);
	}
	
	public static String getValByKey(String key, JSONObject jsonObject) {
		if (BeanUtils.isEmpty(jsonObject) || jsonObject.isEmpty())
			return null;
		return getValByKey(key, jsonObject.toString());
	}
	
	public static JSONObject revertKeyAndVal(JSONObject jo) {
		if (BeanUtils.isNotEmpty(jo) && !jo.isEmpty()) {
			Set<String> keySet = jo.keySet();
			JSONObject revertJo = new JSONObject();
			for (String key : keySet) {
				String val = jo.getString(key);
				if (!StringUtil.isEmpty(val)) {
					revertJo.put(val, key);
				}
			}
			return revertJo;
		}
		return new JSONObject();
	}
	
	/**
	 * 将json数组字符串转成对象列表
	 * @param <T>
	 * @param jsonArray
	 * @param t
	 * @return
	 * @return List<T>
	 * @author xianggang
	 * @date 2019年9月2日 上午10:29:43
	 */
	public static <T> List<T> toList(String jsonArray, Class<T> t) {
		if (StringUtil.isEmpty(jsonArray)) {
			return null;
		}
		JSONArray buttonsArray = JSONArray.parseArray(jsonArray);
		List<T> objList = new ArrayList<>();
		for (int i = 0; i < buttonsArray.size(); i++) {
			JSONObject buttonJo = buttonsArray.getJSONObject(i);
			T obj = buttonJo.toJavaObject(t);
			objList.add(obj);
		}
		return objList;
	}
	
	/**
	 * 将json数组字符串转成对象仅有的那些字段
	 * @param <T>
	 * @param jsonArray
	 * @param t
	 * @return
	 * @return List<T>
	 * @author xianggang
	 * @date 2019年9月2日 上午10:30:01
	 */
	public static <T> String toListString(String jsonArray, Class<T> t) {
		if (StringUtil.isEmpty(jsonArray)) {
			return jsonArray;
		}
		JSONArray buttonsArray = JSONArray.parseArray(jsonArray);
		List<T> objList = new ArrayList<>();
		for (int i = 0; i < buttonsArray.size(); i++) {
			JSONObject buttonJo = buttonsArray.getJSONObject(i);
			T obj = buttonJo.toJavaObject(t);
			objList.add(obj);
		}
		return JSONArray.toJSONString(objList);
	}
}
